﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using CIIS.Common;

namespace CIIS
{
    public partial class BMEidt : BaseForm
    {
        private BMEidt()
        {
            InitializeComponent();
        }
        public BMEidt(object obj)
        {
            InitializeComponent();
            string name = obj.GetType().Name;
            IsPx = checkHavePx(name);
            if (!IsPx)
            {
                textEdit3.Visible = false;
                layoutControlItem5.Text = string.Empty;
            }
            tableName = name;
            var o = obj as Framework.Domain.Entity;
            this.id = o.Id;
            this.textEdit1.Text = GetModelValue("MC0000", obj);
            this.textEdit2.Text = GetModelValue("BH0000", obj);
            this.textEdit3.Text = GetModelValue("PX0000", obj);
        }
        public string GetModelValue(string FieldName, object obj)
        {
            try
            {
                Type Ts = obj.GetType();
                object o = Ts.GetProperty(FieldName).GetValue(obj, null);
                string Value = Convert.ToString(o);
                if (string.IsNullOrEmpty(Value))
                    return null;
                return Value;
            }
            catch
            {
                return null;
            }
        }
        public BMEidt(string type)
        {
            InitializeComponent();
            //Type typeofControl = null;
            //string dllPath = Application.StartupPath + "\\Contract.dll";
            //Assembly ass = Assembly.LoadFrom(dllPath);
            //typeofControl = ass.GetType("Contract.Domain." + type);
            //object obj = Activator.CreateInstance(typeofControl);
            IsPx = checkHavePx(type);
            if (!IsPx)
            {
                textEdit3.Visible = false;
                layoutControlItem5.Text = string.Empty;
            }
            tableName = type;
        }
        private string id
        {
            get;
            set;
        }
        private bool IsPx
        {
            get;
            set;
        }
        private bool checkHavePx(string type)
        {
            Framework.QueryInfo info = new Framework.QueryInfo();
            info.CustomSQL = "select count(1) from information_schema.columns where table_name=:table_name and COLUMN_NAME=:COLUMN_NAME";
            info.Parameters.Add("table_name", type);
            info.Parameters.Add("COLUMN_NAME", "PX0000");
            return System.Convert.ToInt32(dao.Dao.ExecuteScare(info)) > 0;
        }
        private string tableName
        {
            get;
            set;
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Framework.QueryInfo info = new Framework.QueryInfo();
            System.Text.StringBuilder sb = new StringBuilder();
            if (string.IsNullOrEmpty(id))
            {
                sb.Append(string.Format("insert into {0}(MC0000,BH0000", tableName));
                if (IsPx)
                {
                    sb.Append(",PX0000)");
                }
                sb.Append("values(:MC0000,:BH0000");
                if (IsPx)
                {
                    sb.Append(",:PX0000");
                }
                sb.Append(")");
            }
            else
            {
                sb.Append(string.Format("update {0} set MC0000=:MC0000,BH0000=:BH0000", tableName));
                if (IsPx)
                {
                    sb.Append(",PX0000=:PX0000");
                }
                sb.Append(" where Id=:Id");
            }
            info.CustomSQL = sb.ToString();
            info.Parameters.Add("MC0000", textEdit1.Text);
            info.Parameters.Add("BH0000", textEdit2.Text);
            if (IsPx)
            {
                info.Parameters.Add("PX0000", textEdit3.Text);
            }
            if (!string.IsNullOrEmpty(id))
            {
                info.Parameters.Add("Id", id);
            }
            dao.Dao.ExecuteNonQuery(info);
            MessageBox.Show("保存成功！");
            DialogResult = System.Windows.Forms.DialogResult.OK;

        }
    }
}

